﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DAL;

namespace PeisongWeb
{
    public class buyerPage:customerPage
    {
        protected override void OnInit(EventArgs e)
        {
            
            base.OnInit(e);
            //是否为采购商,否则返回到index.html页面
            if (!allowAccess(Convert.ToInt32(companyType.Buyer)))
            {
                string url = Request.Url.ToString();
                Response.Redirect("/index.html");
            }
            
        }

        static bool hasPopedom(string key, int value, string popedom)
        {
            
            return popedom.Contains((value == 2 ? key + value.ToString() : key));
               
        }

        ///判断是否有权限
        ///</summary>
        ///<param name="key">模块parentCode+code</param>
        ///<param name="value">1-查看,2-编辑</param>
        ///<returns>返回True或False,True:有权限,False:无权限</returns>
        public static bool hasPermission(string key, int value)
        {

            CustomerEntity cus = logic.customer.getById(logic.customer.userId);

            //判断是否为管理员，如果是则具有权限，否则校验权限
            if (cus.IsAdmin)
            {
                return true;
            }
            else if (cus.Permissions == "")
            {
                return false;
            }
            else
            {
                return hasPopedom(key, value, cus.Permissions);
            }
        }


        ///<summary>
        ///判断是否有权限
        ///</summary>
        ///<param name="key">模块parentCode+code</param>
        ///<param name="value">1-查看,2-编辑</param>
        ///<param name="queryStringId">0-无用户</param>
        ///<param name="type">"orderId"-订单编号;"userId"-用户Id;"companyId"-公司Id</param>
        ///<returns>返回True或False,True:有权限,False:无权限</returns>
        public static bool hasPermission(string key, int value, int queryStringId, string type)
        {
           
            CustomerEntity cus = logic.customer.getById(logic.customer.userId);

            if (type == "comapanyId")
            {
                if (logic.customer.getCompanyId() != cus.CompanyId)
                {
                    return false;
                }
                else
                    return hasPermission(key, value);
            }
            else if (type == "orderId")
            {
                if (logic.order.getById(queryStringId).CompanyId != cus.CompanyId)
                {
                    return false;
                }
                else
                    return hasPermission(key, value);
            }
            else if (type == "userId")
            {
                if (logic.customer.getById(queryStringId).CompanyId != cus.CompanyId)
                {
                    return false;
                }
                else
                    return hasPermission(key, value);
            }
            else
            {
                return hasPermission(key, value);
            }
                
        }

       

        
    }
}
